/**
 * PackModelAction.java
 * Created at 2017-4-18
 * Created by shihui
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.basic.action;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.utils.ImportExcelUtils;
import com.broadtext.common.utils.RmsUtil;
import com.broadtext.common.utils.UUIDUtils;
import com.broadtext.rms.basic.model.TtPackModel;
import com.broadtext.rms.basic.service.IPackModelService;
import com.broadtext.rms.system.model.TmWoFile;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: PackModelAction</p>
 * <p>Description: 电池型号管理控制层</p>
 * <p>Author: shihui</p>
 * <p>Date: 2017-4-18</p>
 */
@Controller
@RequestMapping("main/packModel")
public class PackModelAction {

    @Autowired
    @Qualifier("packModelServiceImpl")
    private IPackModelService packModelService;
    
    /**
     * 日志信息
     */
    public static Logger logger = LoggerFactory.getLogger(TtPackModel.class);
    
    /**
     * 
     * <p>Description: 获取电池型号列表列表</p>
     * @param request 请求对象
     * @param response 返回对象
     */
    @RequestMapping("/getPackModels.mvc")
    @ResponseBody
    public void getPackModelsList(HttpServletRequest request, HttpServletResponse response) {
        try {
            PageBounds pageBounds;
            pageBounds = new PageBounds();
            Map<String, Object> map;
            String aoData;
            aoData = request.getParameter("aoData");
            JSONArray array;
            array = JSON.parseArray(aoData);
            map = new HashMap<String, Object>();
            if (array != null) {
                Iterator<?> it;
                it = array.iterator();
                while (it.hasNext()) {
                    JSONObject obj;
                    obj = (JSONObject) it.next();
                    if(obj.get("value")!=null && !"".equals(obj.get("value")+"")){
                        map.put(obj.get("name")+"", obj.get("value"));
                    }
                }
            }
            pageBounds.setLimit(Integer.parseInt(map.get("iDisplayLength").toString()));
            pageBounds.setPage(Integer.parseInt(map.get("iDisplayStart").toString()) / pageBounds.getLimit()+1);
            EntityPageBean<TtPackModel> pageBean = this.packModelService.getPackModels(map, pageBounds);
            Map<String, Object> dataMap=new HashMap<String, Object>();
            dataMap.put("recordsTotal", pageBean.getiTotalRecords());
            dataMap.put("recordsFiltered", pageBean.getiTotalRecords());
            dataMap.put("aaData", pageBean.getResults());
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = null;
            try {
                out = response.getWriter();
                out.write(JSON.toJSONString(dataMap));
            } catch (IOException e) {
                logger.error("查询出错",e);
                e.printStackTrace();
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        
    }
    
    /**
     * 
     * <p>Description: 编辑电池型号时查看联系人信息</p>
     * @param request 请求
     * @param pkId 联系人ID
     * @return 联系人信息
     */
    @RequestMapping(value = "getPackModelInfo.mvc")
    @ResponseBody
    public Map<String, Object> getPackModelInfo(HttpServletRequest request, String pkId) {
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        try {
            map.put("data", this.packModelService.findEntityById(pkId));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return map;
    }
    
    /**
     * 
     * <p>Description: 编辑电池型号时查看联系人信息</p>
     * @param request 请求
     * @param pkId 联系人ID
     * @return 联系人信息
     */
    @RequestMapping(value = "getPackModelDetail.mvc")
    @ResponseBody
    public TtPackModel getPackModelDetail(HttpServletRequest request, String pkId) {
        TtPackModel model;
        model = new TtPackModel();
        try {
            model = this.packModelService.findPackModelById(pkId);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return model;
    }
    
    /**
     * 
     * <p>Description: 新增/编辑联系人时查询客户下拉框数据</p>
     * @param request 请求
     * @return 结果
     */
    @RequestMapping(value = "getPackModelList.mvc")
    @ResponseBody
    public Map<String, Object> getPackModelList (HttpServletRequest request) {
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        try {
            map.put("data", this.packModelService.findPackModelList());
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return map;
    }
    
    /**
     * 
     * <p>Description: 保存或更新电池型号</p>
     * @param request 请求
     * @param model 实体
     * @param response 响应
     * @return 结果
     */
    @RequestMapping(value = "saveOrUpdate.mvc")
    @ResponseBody
    public Map<String, Object> saveOrUpdatePackModel (HttpServletRequest request, TtPackModel model, HttpServletResponse response) {
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        
        String sysUser;
        sysUser = String.valueOf(request.getSession().getAttribute("sys.userName"));
        try {
            if (this.packModelService.checkModelExist(model)) {
                map.put("msg", "repeat");
                return map;
            }
            model.setUpdateDate(new Date());
            model.setUpdateUser(sysUser);
            if (!"".equals(model.getPkId())) {
                this.packModelService.updateRecord(model);
            } else {
                //add
            	model.setPkId(UUIDUtils.getUUID());
                model.setDelFlag("0");
                model.setCreateDate(new Date());
                model.setCreateUser(sysUser);
                this.packModelService.insertRecord(model);
            }
            //成功
            map.put("msg", true);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return map;
    }
    
    /**
     * 
     * <p>Description:删除电池型号</p>
     * @param request 请求
     * @param response 响应
     */
    @RequestMapping(value = "/delete.mvc")
    @ResponseBody
    public Map<String, Object> deletePackModel(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        String ids;
        ids = request.getParameter("pkId");
        String[] idArray;
        idArray = ids.split(",");
        String sysUser;
        sysUser = String.valueOf(request.getSession().getAttribute("sys.userName"));
        try {
            map.put("msg", this.packModelService.deleteByIds(idArray, sysUser));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return map;
    }
    
    /**
     * 
     * <p>Description: 初始化电池关联绑定页面</p>
     * @param id packModel主键id
     * @param request 请求对象
     * @param response 返回对象
     */
    @RequestMapping(value = "/initBatteryCorrelation.mvc")
    @ResponseBody
    public void initPackModelCorrelation(String id,HttpServletRequest request, HttpServletResponse response) throws ServiceException {
        Map<String, Object> map = new HashMap<String, Object>();
        map = this.packModelService.initBatteryCorrelation(id);
        RmsUtil.writeJsonStr(response, JSON.toJSONString(map));
    }
    
    /**
     * 
     * <p>Description: 电池关联绑定</p>
     * @param tmPackCustomFactory 电池于客户厂区关联对象
     * @param request 请求对象
     * @param response 返回对象
     */
    @RequestMapping(value = "/saveBatteryCorrelation.mvc")
    @ResponseBody
    public void saveBatteryCorrelation(TtPackModel ttPackModel,HttpServletRequest request, HttpServletResponse response) throws ServiceException {
        String loginUser;
        loginUser = String.valueOf(request.getSession().getAttribute("sys.userName"));
        this.packModelService.saveBatteryCorrelation(ttPackModel, loginUser);
    }
    
    
}
